/**
 * @Author:  renyc
 * @Date: 2022年12月21日21:00:36
 */

import * as Cesium from "cesium/Cesium";
import PrimitiveOverlay from "./PrimitiveOverlay";

/**
 * @class EllipsoidPrimitive
 */
class EllipsoidPrimitive extends PrimitiveOverlay{

    /**
     * 
     * @param {*} ellipse  
     * @param {*} options 
     * @description ellipsoid = {
     *      position : {
     *          lng : 116,
     *          lat : 39,
     *          alt : 15000.0
     *      },
     *      axes: {
     *          x: 300000.0,
     *          y: 200000.0, 
     *          z: 150000.0,
     *      }
     * }
     */
    constructor(ellipsoid, options){
        super();
    
        this._ellipsoid = ellipsoid;
        this._options = options;
        this._delegate = undefined;

        if(this._ellipsoid){
            this._delegate = this._createPrimivive();
        }
    }

    _createPrimivive(){
    
        let position = this._ellipsoid.position;
        let axes = this._ellipsoid.axes;
        var lng = 116.3793; //经度
        var lat = 40.0045; //纬度

        //2. 创建几何对象
        const geometry = new Cesium.EllipsoidGeometry({
          vertexFormat: Cesium.PerInstanceColorAppearance.VERTEX_FORMAT,
          //定义椭球体的x, y, z三轴
          radii: new Cesium.Cartesian3(axes.x, axes.y, axes.z),
        });

        //3.1 创建第1个geometry的坐标系变换矩阵
        //3) 生成变换矩阵
        var modelMatrix = Cesium.Matrix4.multiplyByTranslation(
            //1) 获取椭球体球面位置处的坐标系
            Cesium.Transforms.eastNorthUpToFixedFrame(
                Cesium.Cartesian3.fromDegrees(lng, lat)
            ),
            //2) 定义平移向量，分别是(x, y, z)方向的平移量。          
            new Cesium.Cartesian3(0.0, 0.0, position.alt),
            new Cesium.Matrix4()
        );

        //3.2 创建第1个geometry实例对象
        const instance = new Cesium.GeometryInstance({
            //1) 将geometry几何对象赋予GeometryInstance的geometry属性
            geometry: geometry,
            //2) 将modelMatrix变换矩阵赋予GeometryInstance的modelMatrix属性
            modelMatrix: modelMatrix,
            attributes: {
                color: Cesium.ColorGeometryInstanceAttribute.fromColor(
                    Cesium.Color.CYAN
                ),
            },
        });

        //5. 创建geometry实例数组
        var instances = [instance];
        let apperance = new Cesium.PerInstanceColorAppearance({
            translucent: false,
            closed: true,
          });

        let primitive = new Cesium.Primitive({
              geometryInstances: instances,
              appearance: apperance
        });
        return primitive;
    }
}

export default EllipsoidPrimitive